package roborally.script.singleCommand;

import roborally.model.Direction;
import roborally.model.Energy;
import roborally.model.Robot;
import roborally.script.SingleCommand;
import be.kuleuven.cs.som.annotate.*;
import be.kuleuven.cs.som.taglet.*;

/**
 * @author Joren VERSPEURT
 * @author Mathieu REYMOND
 * @version 3.0
 * 
 * The class representing a Turn command. When stepped it will make its Robot turn in the given Direction (CLOCKWISE or COUNTERCLOCKWISE).
 */
public class Turn extends SingleCommand {
	
	/**
	 * Create a new Turn command with a given Robot and Direction.
	 * @param robot The Robot this Turn command will apply to.
	 * @param direction The direction <code>robot</code> will turn in.
	 */
	public Turn(Robot robot, Direction direction) {
		super(robot);
		this.direction = direction;
	}
	
	/**
	 * Alternate constructor for Turn taking a string for the direction parameter. 
	 * The string is automatically converted to it's Direction value if it is valid.
	 * @param robot The Robot this Turn command will apply to.
	 * @param direction The String representing the direction <code>robot</code> will turn in.
	 */
	public Turn(Robot robot, String direction) {
		this(robot, Direction.valueOf(direction.toUpperCase()));
	}
	
	/**
	 * Return the turning direction this Turn was initialized with.
	 */
	@Basic @Immutable
	public Direction getDirection() {
		return direction;
	}
	
	/**
	 * The Direction to turn in.
	 */
	private final Direction direction;
	
	/**
	 * If <code>robot</code> has enough energy left to turn, turn it in <code>direction</code>.
	 */
	@Override
	public void step() {
		super.step();
		
		if(robot.getEnergy().getValueIn(Energy.Unit.WS) >= Robot.getEnergyRequiredToTurn().getValueIn(Energy.Unit.WS)) {
			robot.turn(direction);
		}
	}
	
	/**
	 * The script word for this command followed by it's Direction parameter.
	 */
	@Override
	public String toString() {
		return "turn " + direction.toString();
	}
}
